package pattern.structrual.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 组和类对象，组合每一个组件，让每个组合类对象都有相同的功能
 *
 * @Author Gavin
 * @date 2020.07.17 10:54
 */
public class Composite extends Component {
    private List<Component> child;
    private String name;

    public Composite(String name) {
        this.name = name;
        child = new ArrayList<>();
    }

    @Override
    void doOperation() {
        throw new UnsupportedOperationException();
    }

    @Override
    void add(Component component) {
        child.add(component);
    }

    @Override
    void remove(Component component) {
        child.remove(component);
    }

    @Override
    Component getChild(int i) {
        return child.get(i);
    }

    @Override
    public void print(int level) {
        for (int i = 0; i < level; i++) {
            System.out.print("--");
        }
        System.out.println("Composite:" + name);
        for (Component component : child) {
            component.print(level + 1);
        }
    }
}
